package com.dd.cloud.user.mq;

import com.dd.cloud.common.constants.IntegralConstants;
import com.dd.cloud.user.service.doctor.IDoctorIntegralService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author xt
 */
@Slf4j
@Component
public class integralMessage {

    @Autowired
    private IDoctorIntegralService doctorIntegralService;

    @RabbitHandler
    @RabbitListener(queues = MQConfig.INTEGRAL_QUEUE)
    public void processHandler(Channel channel, Message message, String params) throws IOException {
        try {
            log.info("开始分发积分,参数为{}", params);
            String[] arrayStr = params.split("-");
            if (arrayStr.length == 2) {
                Integer type = Integer.parseInt(arrayStr[0]);
                //可能为医生id,订单号,处方号
                String param = arrayStr[1];
                if (IntegralConstants.IntegralType.ONE_TYPE.equals(type)) {
                    log.info("注册积分奖励");
                    Integer doctorId = Integer.parseInt(param);
                    doctorIntegralService.registrationBonus(type, doctorId);
                } else if (IntegralConstants.IntegralType.TWO_TYPE.equals(type)) {
                    log.info("完成认证审核");
                    Integer doctorId = Integer.parseInt(param);
                    doctorIntegralService.fishedChecked(type, doctorId);
                } else if (IntegralConstants.IntegralType.THREE_TYPE.equals(type)) {
                    log.info("完善个人信息积分奖励(填写个人简介)");
                    Integer doctorId = Integer.parseInt(param);
                    doctorIntegralService.fishedInfo(type, doctorId);
                } else if (IntegralConstants.IntegralType.SEVENTEEN_TYPE.equals(type)) {
                    log.info("完善个人信息积分奖励(上传头像)");
                    Integer doctorId = Integer.parseInt(param);
                    doctorIntegralService.uploadHeadImg(type, doctorId);
                } else if (IntegralConstants.IntegralType.FOUR_TYPE.equals(type)) {
                    log.info("邀请医生积分奖励(邀请医生注册)");
                    Integer doctorId = Integer.parseInt(param);
                    doctorIntegralService.inviteDoctor(type, doctorId);
                } else if (IntegralConstants.IntegralType.FIVE_TYPE.equals(type)) {
                    log.info("邀请医生积分奖励(邀请医生完成认证并审核)");
                    Integer doctorId = Integer.parseInt(param);
                    doctorIntegralService.inviteDoctor(type, doctorId);
                } else if (IntegralConstants.IntegralType.SIX_TYPE.equals(type)) {
                    log.info("邀请患者积分奖励(手动添加患者信息1人次)");
                    Integer doctorId = Integer.parseInt(param);
                    doctorIntegralService.inviteUserFamily(type, doctorId);
                } else if (IntegralConstants.IntegralType.SEVEN_TYPE.equals(type)) {
                    log.info("邀请患者完成填写信息1人次");
                    Integer doctorId = Integer.parseInt(param);
                    doctorIntegralService.inviteUserFamily(type, doctorId);
                } else if (IntegralConstants.IntegralType.EIGHT_TYPE.equals(type)) {
                    log.info("使用智能辨证开方（学习）1次");
                    Integer doctorId = Integer.parseInt(param);
                    doctorIntegralService.fishedStudyPres(type, doctorId);
                } else if (IntegralConstants.IntegralType.NINE_TYPE.equals(type)) {
                    log.info("使用智能辨证开方（完成闭环）1次");
                    log.info("订单号:{}", param);
                    doctorIntegralService.fishedSmartPres(type, param);
                } else if (IntegralConstants.IntegralType.TEN_TYPE.equals(type)) {
                    log.info("使用自主开方完成闭环1次");
                    log.info("订单号:{}", param);
                    doctorIntegralService.fishedSmartPres(type, param);
                } else if (IntegralConstants.IntegralType.ELEVEN_TYPE.equals(type)) {
                    log.info("阅读书籍单次5分钟及以上1次");
                    Integer doctorId = Integer.parseInt(param);
                    log.info("医生id:{}", doctorId);
                    doctorIntegralService.readIntegral(type, doctorId);
                } else if (IntegralConstants.IntegralType.TWELVE_TYPE.equals(type)) {
                    log.info("查看医案单次5分钟及以上1次");
                    Integer doctorId = Integer.parseInt(param);
                    log.info("医生id:{}", doctorId);
                    doctorIntegralService.readTaskIntegral(doctorId, type);
                } else if (IntegralConstants.IntegralType.THIRTEEN_TYPE.equals(type)) {
                    log.info("查看中草药单次5分钟及以上1次");
                    Integer doctorId = Integer.parseInt(param);
                    log.info("医生id:{}", doctorId);
                    doctorIntegralService.readTaskIntegral(type, doctorId);
                } else if (IntegralConstants.IntegralType.FOURTEEN_TYPE.equals(type)) {
                    log.info("查看方剂单次5分钟及以上1次");
                    Integer doctorId = Integer.parseInt(param);
                    log.info("医生id:{}", doctorId);
                    doctorIntegralService.readTaskIntegral(type, doctorId);
                } else if (IntegralConstants.IntegralType.FIFTEEN_TYPE.equals(type)) {
                    log.info("观看视频单次5分钟及以上1次");
                    Integer doctorId = Integer.parseInt(param);
                    log.info("医生id:{}", doctorId);
                    doctorIntegralService.readIntegral(type, doctorId);
                } else if (IntegralConstants.IntegralType.SIXTEEN_TYPE.equals(type)) {
                    log.info("每日登陆（连续一周登陆额外给予积分）");
                    Integer doctorId = Integer.parseInt(param);
                    log.info("医生id:{}", doctorId);
                    doctorIntegralService.login(type, doctorId);
                } else if (IntegralConstants.IntegralType.EIGHTEEN_TYPE.equals(type)) {
                    log.info("分享行为（内容、线上活动、APP、小程序）1次");
                    Integer doctorId = Integer.parseInt(param);
                    log.info("医生id:{}", doctorId);
                    doctorIntegralService.share(type, doctorId);
                } else if (IntegralConstants.IntegralType.NINETEEN_TYPE.equals(type)) {
                    log.info("处方退款回收积分");
                    log.info("订单号:{}", param);
                    doctorIntegralService.refund(param);
                } else if (IntegralConstants.IntegralType.TWENTY_ONE.equals(type)) {
                    log.info("查看圈子内容");
                    Integer doctorId = Integer.parseInt(param);
                    doctorIntegralService.view(doctorId, type);
                } else if (IntegralConstants.IntegralType.TWENTY_TWO.equals(type)) {
//                    log.info("点赞圈子");
//                    Integer doctorId = Integer.parseInt(param);
//                    doctorIntegralService.like(doctorId, type);
                } else if (IntegralConstants.IntegralType.TWENTY_FOUR.equals(type)
                        || IntegralConstants.IntegralType.TWENTY_FIVE.equals(type)
                        || IntegralConstants.IntegralType.TWENTY_SIX.equals(type)) {
                    log.info("会员建奖励");
                    Integer doctorId = Integer.parseInt(param);
                    doctorIntegralService.award(doctorId, type);
                }
            }
            //手动确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            if (message.getMessageProperties().getRedelivered()) {
                // 拒绝消息
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                e.printStackTrace();
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
        }
    }

}
